__author__ = "Chengye zhao (chengyezhao@gmail.com)"

#from pymmseg import mmseg
from google.appengine.ext import db
from google.appengine.api import urlfetch
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from xml.dom import minidom
import logging

class Category(db.Model):
	c1 = db.IntegerProperty()
	c2 = db.IntegerProperty()
	c3 = db.IntegerProperty()
	prob_c1 = db.FloatProperty()
	prob_c2 = db.FloatProperty()
	prob_c3 = db.FloatProperty()

	def process(self, t):
		self.prob_c1 = 0.0
		self.prob_c2 = 0.0
		self.prob_c3 = 0.0
		if t != 0:
			self.prob_c1 = float(self.c1) / t
			self.prob_c2 = float(self.c2) / t 
			self.prob_c3 = float(self.c3) / t 

class Word(db.Model):
	content = db.StringProperty()
	total_appear = db.IntegerProperty()
	appear_c1 = db.IntegerProperty()
	appear_c2 = db.IntegerProperty()
	appear_c3 = db.IntegerProperty()
	prob = db.FloatProperty()
	prob_c1 = db.FloatProperty()
	prob_c2 = db.FloatProperty()
	prob_c3 = db.FloatProperty()

	def process(self, t, c1, c2, c3):
		self.prob = 0.0
		self.prob_c1 = 0.0
		self.prob_c2 = 0.0
		self.prob_c3 = 0.0
		if t != 0:
			self.prob = float(self.total_appear) / t
		if c1 != 0:
			self.prob_c1 = float(self.appear_c1) / c1
		if c2 != 0:
			self.prob_c2 = float(self.appear_c2) / c2
		if c3 != 0:
			self.prob_c3 = float(self.appear_c3) / c3

def getText(nodelist):
	rc = []
	for node in nodelist:
		if node.nodeType == node.TEXT_NODE or node.nodeType == 4 :
			rc.append(node.data)
	return ''.join(rc)

def getTextNodeText(parent, tag):
	if len(parent.getElementsByTagName(tag)) > 0:
		return getText(parent.getElementsByTagName(tag)[0].childNodes)
	return ''

def getTextNodeFloat(parent, tag):
	try:
		return float(getTextNodeText(parent,tag))
	except TypeError:
		return None
	except ValueError:
		return None

def getTextNodeInt(parent, tag):
	try:
		return int(getTextNodeText(parent, tag))
	except TypeError:
		return None
	except ValueError:
		return None

class SaveClassifierJob(webapp.RequestHandler):
	def get(self):
		#Fetch xml content
		url = "http://173.245.70.96:7778/classifier.xml"
		try:
			result = urlfetch.fetch(url=url)
		except urlfetch.Error:
			logging.error('XML %s cannot connect',  url)
			return 
		logging.info('Load xml from %s' % url)

		q = db.GqlQuery("SELECT * from Word")
		for word in q:
			word.delete()
		logging.info('Old Word info delete')

		q = db.GqlQuery("SELECT * from Category")
		for category in q:
			category.delete()
		logging.info('Old Category info delete')

		dom = minidom.parseString(result.content)
		logging.info('dom paresed')

		words = dom.getElementsByTagName("words")[0]
		category = Category()
		category.prob_c1 = getTextNodeFloat(words, 'prob_c1')
		category.prob_c2 = getTextNodeFloat(words, 'prob_c2')
		category.prob_c3 = getTextNodeFloat(words, 'prob_c3')
		category.put()

		wordlist = words.getElementsByTagName("word")
		for word in wordlist:
			w = Word()
			w.content = getTextNodeText(word, 'content')
			w.prob = getTextNodeFloat(word, 'prob')
			w.prob_c1 = getTextNodeFloat(word, 'prob_c1')
			w.prob_c2 = getTextNodeFloat(word, 'prob_c2')
			w.prob_c3 = getTextNodeFloat(word, 'prob_c3')
			w.put()

application = webapp.WSGIApplication(
	[('/save_classifier', SaveClassifierJob)],
	debug=True)

def main():
	run_wsgi_app(application)

if __name__ == "__main__":
	main()
